package com.sansui.blog.controller.admin.page;

import com.sansui.blog.pojo.Notice;
import com.sansui.blog.pojo.User;
import com.sansui.blog.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @name: NoticeController
 * @author: Sansui
 * @Software: IDEA
 * @Blog: https://www.cnblogs.com/sansui6/
 * @date: 2022-04-15 21:09
 **/
// 公告发布 控制器类
@Controller
@RequestMapping("/admin")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @GetMapping("page/notice")
    public String adminNotice(){
        return "admin/page/notice";
    }

    //  打开添加公告页面
    @GetMapping("page/notice/add")
    public String addNoticeHtml(){
        return "admin/page/notice/add";
    }

    //  打开编辑公告页面
    @GetMapping("page/notice/check")
    public String checkNoticeHtml(){
        return "admin/page/notice/check";
    }


    /**
     * 获取公告列表
     * @param page
     * @param limit
     * @return
     */
    @PostMapping("page/getNoticeList")
    @ResponseBody
    public Map<String, Object> getNoticeList(Integer page, Integer limit){
        Map<String, Object> map = noticeService.getNoticeList(page,limit);
        return map;
    }

    /**
     * 修改公告状态
     * @param noticeId
     * @param status
     * @return
     */
    @PostMapping("page/notice/updateStatus")
    @ResponseBody
    public Map<String, Object> updateStatus(Long noticeId , String status){
        Map<String, Object> map = noticeService.updateStatus(noticeId, status);
        return map;
    }

    /**
     * 删除公告
     * @param noticeId
     * @return
     */

    @PostMapping("page/notice/deleteNotice")
    @ResponseBody
    public Map<String, Object> deleteNotice(Long noticeId){
        Map<String, Object> map = noticeService.deleteNotice(noticeId);
        return map;
    }

    /**
     * 增加公告
     * @param notice
     * @param session
     * @return
     */
    @PostMapping("page/notice/create")
    @ResponseBody
    public Map<String, Object> addNotice(@RequestBody Notice notice, HttpSession session){
//        User user = (User) session.getAttribute("user");
        Map<String,Object> data = (Map<String, Object>) session.getAttribute("user");
        Map<String, Object> map = noticeService.addNotice(notice,String.valueOf(data.get("username")));
        return map;
    }

    // 批量删除博客
    @PostMapping("page/notice/deleteNoticeByIds")
    @ResponseBody
    public Map<String, Object> deleteNoticeByIds(String noticeIds){
        List<String> delList = new ArrayList<>();
        String[] nIds = noticeIds.split(",");
        for (String str : nIds) {
            delList.add(str);
        };
        Map<String, Object> map = noticeService.deleteNoticeByIds(delList);
        return map;
    }

    @PostMapping("page/notice/noticeSearch")
    @ResponseBody
    public Map<String, Object> noticeSearch(String notitle){
        Map<String, Object> map = noticeService.noticeSearch(notitle);
        return map;
    }


}
